package com.jiukuaidao.zhulin.module.order.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.api.ApiConfirmOrder;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.entity.ApiConfirmOrderInfo;
import com.jiukuaidao.zhulin.entity.EventShoppingCartInfo;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.order.PostOrderInfo;
import com.jiukuaidao.zhulin.module.order.contract.ConfirmOrderContract;
import com.ta.utdid2.android.utils.StringUtils;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/6
 * Description :
 */
public class ConfirmOrderPresenter extends BasePresenter<ConfirmOrderContract.View, TasksRepository> implements ConfirmOrderContract.Presenter {
    public ConfirmOrderPresenter(TasksRepository repository, ConfirmOrderContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {
        confirmOrder();
    }

    public void confirmOrder() {
        mRepository.loadData(new ApiConfirmOrder(paramsCommitOrder(), new MyRetroCallBack<ApiConfirmOrderInfo>() {
            @Override
            public void onSuccess(ApiConfirmOrderInfo result, String errorCode, String msg) {
                mView.getApiInfo(result);
                mView.toast(msg);
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                mView.toast(msg);
            }

            @Override
            public void onFail(ApiConfirmOrderInfo result, String errorCode, String msg) {
                if (result.needConfirm != null) {
                    mView.getNeedConfirm(result.needConfirm, msg);
                }
                mView.toast(msg);
            }
        }));

    }


    @Override
    public PostOrderInfo paramsCommitOrder() {
        PostOrderInfo params = new PostOrderInfo();
        params.shopId = mView.getShopId();//商家id，必填
        if (!StringUtils.isEmpty(mView.getInvoice())) {
            params.invoice = true; //是否需要发票，必填参数
            params.invoiceTitle = mView.getInvoice(); //发票抬头，invoice为true时必填
        } else {
            params.invoice = false; //是否需要发票，必填参数
            params.invoiceTitle = ""; //发票抬头，invoice为true时必填
        }
        params.channel = "1";  //订单渠道，必填
        params.payWay = mView.getPayWay(); //支付方式，必填
        params.consignee = mView.getAddressInfo().consignee;//收货人姓名，必填参数
        params.consigneePhone = mView.getAddressInfo().mobile; //收货人联系电话，必填参数
        params.consigneeAddress = mView.getAddressInfo().address;//收货地址，必填参数
        params.consigneeGeo = mView.getAddressInfo().longitude + "," + mView.getAddressInfo().latitude; //收货地址经纬度，逗号隔开，必填参数
        params.remark = mView.getRemark();//订单备注，可选参数
        PostOrderInfo.PackList packList = new PostOrderInfo.PackList();
        packList.checked = "true";
        packList.count = 1;
        packList.name = "";
        packList.src = "";
        params.cart.packList.add(packList);
        if (mView.getEventShoppingCartDatas() == null)
            return null;

        for (int i = 0; i < mView.getEventShoppingCartDatas().size(); i++) {
            EventShoppingCartInfo result = mView.getEventShoppingCartDatas().get(i);
            PostOrderInfo.CartItem cartItem = new PostOrderInfo.CartItem();
            cartItem.count = result.getCount();
            cartItem.gn = result.getGoodsGN();
            cartItem.checked = "true";
            cartItem.src = "";
            params.cart.packList.get(0).cartItems.add(cartItem);
        }
        if (mView.getIgnoreList() != null) {
            for (int i = 0; i < mView.getIgnoreList().size(); i++) {
                params.confirmedTokens.add(mView.getIgnoreList().get(i));
            }
        }
        return params;
    }
}
